package com.yangrong.webcrawler.schedule.impl;

import com.yangrong.webcrawler.entity.NewsEntity;
import com.yangrong.webcrawler.schedule.NewsPuller;
import com.yangrong.webcrawler.service.NewsService;
import com.yangrong.webcrawler.utils.SaveNews;
import org.apache.commons.lang.StringUtils;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service("newsPullerImpl")
public class NewsPullerImpl implements NewsPuller {

    private static final Logger logger = LoggerFactory.getLogger(NewsPullerImpl.class);

    private static final String url = "https://www.wewhale.cn/";

    private static final String HzUrl = "https://alternative.me/crypto/";

    @Autowired
    private NewsService wjListService;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Override
    public void pullNews() {

        logger.info("开始拉去数据......");
        // 获取首页
        Document html = null;
        try {
            html = getHtmlFromUrl(url, false);
        } catch (Exception e) {
            logger.error("获取首页失败：{}，错误信息：{}", url, e.getMessage());
            return;
        }

        // jsoup获取新闻<a>标签
        Elements elements = html.select("div.list-home")
                .select("div.list-item");

        // 从<a>标签中抽取基本信息，封装成news
        NewsEntity news = null;
        List<NewsEntity> newsHashSet = new ArrayList<>();
        for (Element element : elements) {
            String url = element.select("div.list-content")
                    .select("div.list-body")
                    .select("a")
                    .attr("href");
            String title = element.select("div.list-content")
                    .select("div.list-body")
                    .select("a")
                    .attr("title");
            String outline = element.select("div.list-content")
                    .select("div.list-body")
                    .select("div.list-desc")
                    .select("div.h-2x")
                    .text();
            String img = element.select("div.media")
                    .select("a")
                    .attr("data-bg")
                    .replace("url('", "")
                    .replace("')", "");
            String newsDate = element.select("div.list-footer")
                    .select("div")
                    .select("time.mx-1")
                    .text();
            String label = element.select("div.list-content")
                    .select("div.list-body")
                    .select("span.badge")
                    .text();
            String allType = element.select("div.list-footer")
                    .select("div.d-inline-block")
                    .select("a")
                    .text();

            String typeHeader = "";
            String type = "";
            if (StringUtils.isNotBlank(allType)) {
                String[] spString = allType.split("\\s+");
                for (int i = 0; i < spString.length; i++) {
                    if (i == 0) {
                        typeHeader = spString[i];
                    } else if (i == 1) {
                        type = spString[i];
                    }
                }
            }

            news = new NewsEntity();
            news.setOutline(outline);
            news.setUrl(url);
            news.setTitle(title);
            news.setCreateDate(new Date());
            news.setNewsDate(newsDate);
            news.setImage(img);
            news.setLabel(label);
            news.setTypeHeader(typeHeader);
            news.setType(type);
            newsHashSet.add(news);
        }

        // 根据新闻url访问新闻，获取新闻内容
        newsHashSet.forEach(oneNew -> {
            logger.info("开始获取新闻内容：{}", oneNew.getUrl());
            Document newsHtml = null;
            try {
                newsHtml = getHtmlFromUrl(oneNew.getUrl(), false);
                Element newsContent = newsHtml.select("div.container")
                        .select("div.post-content")
                        .select("div.nc-light-gallery")
                        .first();
                String content = newsContent.toString();

                // 处理新闻名称，去除特殊符号及空格
                String regEx = "[\n`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。， 、？]";
                String fileName = oneNew.getTitle().replaceAll(regEx, "").trim().concat(".txt");

                SaveNews save = new SaveNews();
                save.saveAsFileWriter(content, fileName);

                oneNew.setContent(fileName);

                Thread.sleep(2000);

            } catch (Exception e) {
                logger.error("新闻获取失败：{}，错误信息：{}", oneNew.getUrl(), e.getMessage());
            }
        });

        wjListService.saveBatch(newsHashSet);

    }

    @Override
    public void getHz() {
        logger.info("开始获取52Hz......");
        // 获取首页
        Document html = null;
        try {
            html = getHtmlFromUrl(HzUrl, false);
        } catch (Exception e) {
            logger.error("获取52Hz失败：{}，错误信息：{}", HzUrl, e.getMessage());
            return;
        }

        Elements elements = html.select("div.columns");
        Element element = elements.get(0).select("div.column").get(1).select("div.box").select("div.block").select("div.fng-value").get(0);

        String HzValue = element.select("div.fng-circle").text();
        stringRedisTemplate.opsForValue().set("HzValue", HzValue);
        logger.info("获取52Hz成功：{}", HzValue);
    }
}
